﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Data;
using System.Linq;
using System.ServiceModel.DomainServices.EntityFramework;
using System.ServiceModel.DomainServices.Hosting;
using System.ServiceModel.DomainServices.Server;
using System.ServiceModel.DomainServices.Server.ApplicationServices;
using MoneyBook.Web.Models;
using System.Web.Security;
using System.Web.Configuration;
using System.Security.Principal;

namespace MoneyBook.Web.Services {

    [EnableClientAccess()]
    public class MoneyBookAuthenticationService : DomainService,IAuthentication<User> {

        public User GetUser() {

            IPrincipal currentUser = this.ServiceContext.User;
            if (currentUser != null) {
                string userName = currentUser.Identity.Name;
                
                //TODO : 要変更。ログイン中にDBの行が消されていた場合などの対応で、DB以外に格納しておく必要がある
                using (MoneyBookEntities context = new MoneyBookEntities()) {
                    Member member = context.Members.SingleOrDefault(m => m.MemberName == userName);
                    if (member != null) {
                        return new User() {
                            Name = member.MemberName,
                            Roles = member.IsAdmin ? new string[] { "Admin" } : null,
                            MemberID = member.MemberId,
                            DisplayName = member.DisplayName,
                            MainAccountId = member.MainAccountId
                        };
                    }
                }
            }
            return new User();
        }

        public User Login(string userName, string password, bool isPersistent, string customData) {
            User user = GetUser(userName, password);
            if (user != null) {
                FormsAuthentication.SetAuthCookie(userName, isPersistent);
            }
            return user;
        }

        public User Logout() {
            FormsAuthentication.SignOut();
            return new User();
        }

        public void UpdateUser(User user) {
            throw new NotImplementedException();
        }

        private User GetUser(string userName, string password) {

            string passwordHash = Authentication.GetPasswordHash(password);
            using (MoneyBookEntities context = new MoneyBookEntities()) {
                Member member = context.Members.SingleOrDefault(m => m.MemberName == userName && m.PasswordHash == passwordHash);
                if (member == null) {
                    return null;
                }
                return new User() {
                    Name = member.MemberName,
                    Roles = member.IsAdmin ? new string[] { "Admin" } : null,
                    MemberID = member.MemberId,
                    DisplayName = member.DisplayName,
                    MainAccountId = member.MainAccountId
                };
            }
        }

    }
}


